<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    html,
    body {
        width: 100%;
        height: 100%;
    }

    ul {
        list-style: none;
    }

    button {
        padding: 5px 10px;
    }


    button i {
        width: 14px;
        height: 14px;
        display: inline-block;
        background-image: url(./定位.png);
        background-size: contain;
        vertical-align: middle;
    }

    button span {
        vertical-align: middle;
    }

    .mask {
        width: 100vw;
        height: 100vh;
        position: fixed;
        left: 0;
        top: 0;
        background-color: rgba(0, 0, 0, .5);
        display: none;
    }

    .main {
        width: 900px;
        height: 670px;
        position: absolute;
        left: 50%;
        top: 50%;
        background-color: #fff;
        transform: translate(-50%, -50%);
        position: relative;
        padding-top: 76px;
        padding-bottom: 76px;

        display: flex;
    }


    .top {
        width: 100%;
        height: 76px;
        position: absolute;
        left: 0;
        top: 0;
    }

    .top h1 {
        font-size: 20px;
        font-weight: 400;
        padding: 25px 40px;
        text-align: center;
    }

    .top span {
        float: right;
        width: 14px;
        height: 14px;
        background-image: url(./Shape.png);
        margin-top: 10px;
        cursor: pointer;
    }

    .main .left {
        width: 40px;
        height: 460px;
        margin-top: 30px;
        text-align: center;
    }

    .left li {
        height: 20px;
        line-height: 20px;
        font-size: 12px;
        color: #333;
        cursor: pointer;
    }

    .botton {
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 76px;
        box-shadow: 0px -2px 6px 0px rgba(0, 18, 103, 0.07);
        border: 1px solid #F2F2F2;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .btn {
        width: 200px;
        display: flex;
        justify-content: space-evenly;
    }

    .btn button {
        width: 72px;
        height: 40px;
        cursor: pointer;
        border-radius: 4px;
        border: none;
    }

    .btn button:nth-child(1) {
        background-color: #5685FE;
        color: #fff;
    }

    .btn button:nth-child(2) {
        background-color: #fff;
        border: 1px solid #DBDBDB;
    }


    .right {
        flex: 1;
        overflow: auto;
        border-left: 1px solid #f5f5f5;
        border-top: 1px solid #f5f5f5;
    }


    .right li h3 {
        background-color: #EFF2F9;
        opacity: .5;
        padding-left: 20px;
        font-size:
            16px;
        font-weight: 400;
    }


    .right li .content {
        padding: 20px;
        display: flex;
        flex-wrap: wrap;
    }

    .right li .content div {
        width: 205px;
    }

    .right li .content .sp {
        padding: 7px 20px;
        display: inline-block;
        font-size: 14px;
        cursor: pointer;
    }

    .bg-bule {
        color: rgb(86, 133, 254);
        background-color: rgb(245, 247, 254);
    }

    .show {
        display: block;
    }

    a {
        text-decoration: none;
    }
</style>

<body>
    <button><i></i><span>定位</span></button>
    <div class="mask">
        <div class="main">
            <div class="top">
                <h1>选择城市<span></span></h1>
            </div>

            <div class="left">
                <ul>
                    <li><a href="#1">#</a></li>
                </ul>
            </div>

            <div class="right">
                <ul>
                    <li>
                        <h3 id="1">#</h3>
                        <div class="content">
                            <div><span class="bg-bule sp">全国</span></div>
                        </div>
                    </li>

                </ul>
            </div>

            <div class="botton">
                <div class="btn">
                    <button id="yes">确定</button>
                    <button id="no">取消</button>

                </div>
            </div>
        </div>
    </div>
    <script src="./cities.js"></script>
    <script>
        // 获取弹窗
        const mask = document.getElementsByClassName('mask')[0]
        // 获取左侧导航
        const left = document.getElementsByClassName('left')[0]
        // 获取右侧地名盒子
        const right = document.getElementsByClassName('right')[0]

        // 渲染左侧导航和右侧地名框架
        for (const k in city) {
            let li1 = document.createElement('li')
            li1.innerHTML = `
                <a href="#${k}">${k}</a>
            `
            left.children[0].append(li1)

            let li2 = document.createElement('li')
            li2.innerHTML = `
                    <h3 id="${k}">${k}</h3>
                    <div class="content">      
                    </div>
                `
            right.children[0].append(li2)

        }

        // 渲染地名
        const lis = Array.from(right.children[0].children)
        lis.forEach((v, i) => {
            if (i > 0) {
                for (let i = 0; i < city[v.children[0].innerHTML].length; i++) {
                    let div = document.createElement('div')
                    div.innerHTML = `
                        <sapn class="sp">${city[v.children[0].innerHTML][i].name}</span>
                    `

                    v.children[1].append(div)
                }
            }
        })

        const spans = Array.from(document.getElementsByClassName('sp'))

        //  地名点击效果
        let index = 0
        spans.forEach((v, i) => {
            v.onclick = () => {
                document.getElementsByClassName('bg-bule')[0].classList.remove('bg-bule')
                v.classList.add('bg-bule')
                index = i
            }
        })

        // 获取按钮
        const btn1 = document.getElementById('yes')
        const btn2 = document.getElementById('no')
        const btn3 = document.getElementsByTagName('button')[0]
        let noi = 0
        // 点击显示隐藏
        btn1.onclick = () => {
            console.log(1);
            mask.classList.remove('show')
            btn3.children[1].innerHTML = spans[index].innerHTML
            noi = index
        }

        btn2.onclick = () => {
            mask.classList.remove('show')
            document.getElementsByClassName('bg-bule')[0].classList.remove('bg-bule')
            spans[noi].classList.add('bg-bule')
        }

        btn3.onclick = () => {
            mask.classList.add('show')
        }

    </script>
</body>

</html>